package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.emun.UseStatus;
import com.ray.depot.service.InOutSerializeService;
import com.ray.depot.table.customMapper.SelectInOutSerializeMapper;
import com.ray.depot.table.dto.InOutSerializeDto;
import com.ray.depot.table.entity.InOutSerialize;
import com.ray.depot.table.entity.InOutSerializeExample;
import com.ray.depot.table.entity.InOutSerializeExample.Criteria;
import com.ray.depot.table.mapper.InOutSerializeMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class InOutSerializeServiceImpl extends BaseService implements InOutSerializeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(InOutSerializeServiceImpl.class);
	
	@Autowired
	private InOutSerializeMapper inOutSerializeMapper;
	@Autowired
	private SelectInOutSerializeMapper selectInOutSerializeMapper;

	@Transactional
	public Result<Object> addInOutSerialize(InOutSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=inOutSerializeMapper.insertSelective(params);
		if(count!=1){
			logger.error("InOutSerializeServiceImpl=>addInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public InOutSerialize queryInOutSerialize(String id, LoginUser user) {
		return inOutSerializeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateInOutSerialize(InOutSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=inOutSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("InOutSerializeServiceImpl=>editInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteInOutSerialize(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		InOutSerialize params=new InOutSerialize();
		params.setId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=inOutSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("InOutSerializeServiceImpl=>deleteInOutSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<InOutSerialize> queryInOutSerialize(InOutSerialize params, PageDto<InOutSerialize> page, LoginUser user) {
		InOutSerializeExample example=new InOutSerializeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}

		if(StrUtil.isNotBlank(params.getInOutNo())){
			criteria.andInOutNoEqualTo(params.getInOutNo());
		}
		
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(inOutSerializeMapper.countByExample(example));
		page.setRecords(inOutSerializeMapper.selectByExample(example));
		return page;
	}


	@Override
	public void deleteInOutSerializeByNo(String inOutNo, LoginUser user) {
		InOutSerialize params=new InOutSerialize();
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		InOutSerializeExample example=new InOutSerializeExample();
		 example.createCriteria().andInOutNoEqualTo(inOutNo);
		 inOutSerializeMapper.updateByExampleSelective(params, example);
	}

	 @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<InOutSerializeDto> querySelectInOutSerialize(InOutSerializeDto params, PageDto<InOutSerializeDto> page, LoginUser user) {
	    //params.setTenantId(user.getTenantId());
	    params.setOrderByClause(page.getOrderBy());
	    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
	    params.setLimitEnd(page.getPageSize());
	    params.setMaterialStatus(UseStatus.ON.getType());
	    params.setIsDelete(DeleteEnum.USE.getValue());
		page.setTotal(selectInOutSerializeMapper.countByExample(params));
		page.setRecords(selectInOutSerializeMapper.selectByExample(params));
		return page;
	}
	 
	 @Override
	public List<InOutSerialize> queryInOutSerializesByInOutNo(String inOutNo, LoginUser user) {
		InOutSerialize params = new InOutSerialize();
		params.setInOutNo(inOutNo);
		params.setIsDelete(DeleteEnum.USE.getValue());
		return inOutSerializeMapper.selectByRecord(params);
	}
}
